package com.lemon.smartsafetynew.ui.adapter.record;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.record.SafeTrainVo;
import com.lemon.smartsafetynew.util.common.CommonUtil;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import kale.adapter.LemonRcvAdapter;
import kale.adapter.item.AdapterItem;

/**
 * Created by JiangAn on 2018/5/25.
 */

public class SafeTrainAdapter extends LemonRcvAdapter<SafeTrainVo> {
    private RecyclerTClickListener<SafeTrainVo> mListener;

    public SafeTrainAdapter(@Nullable List<SafeTrainVo> data, RecyclerTClickListener<SafeTrainVo> mListener) {
        super(data);
        this.mListener = mListener;
    }

    @NonNull
    @Override
    public AdapterItem createItem(Object type) {
        return new SafeTrainAdapter.SafeTrainItem();
    }

    public class SafeTrainItem implements AdapterItem<SafeTrainVo> {
        @Bind(R.id.tv_space_name)
        TextView tvSpaceName;
        @Bind(R.id.tv_peopel_type)
        TextView tvPeopelType;
        @Bind(R.id.tv_peopel_duty)
        TextView tvPeopelDuty;
        @Bind(R.id.tv_certificate_origin)
        TextView tvCertificateOrigin;
        @Bind(R.id.tv_laster_train_date)
        TextView tvLasterTrainDate;
        @Bind(R.id.tv_date)
        TextView tvDate;
        @Bind(R.id.item_layout)
        LinearLayout itemLayout;
        @Bind(R.id.frameLayout)
        FrameLayout frameLayout;
        private int pos;
        private SafeTrainVo data;

        @Override
        public int getLayoutResId() {
            return R.layout.layout_safe_train_item;
        }

        @Override
        public void bindViews(View root) {
            ButterKnife.bind(this, root);
        }

        @Override
        public void setViews() {
            itemLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mListener.onItemClick(data, pos);
                }
            });
        }

        @Override
        public void handleData(SafeTrainVo safeTrainVo, int position) {
            data = safeTrainVo;
            pos = position;
            tvSpaceName.setText(CommonUtil.avoidNullString(safeTrainVo.getTrainee_name()));
            tvPeopelType.setText(CommonUtil.avoidNullString(safeTrainVo.getTrainee_type()));
            tvPeopelDuty.setText(CommonUtil.avoidNullString(safeTrainVo.getTrainee_position()));
            tvCertificateOrigin.setText(CommonUtil.avoidNullString(safeTrainVo.getIssue_gov()));
            if (safeTrainVo.getRecent_date() != null) {
                tvLasterTrainDate.setText(CommonUtil.secondToTimeStr(safeTrainVo.getRecent_date(), DateTypeEnum.DAY));
            }
            if(safeTrainVo.getDate_flag() != null){
                int status = safeTrainVo.getDate_flag(); //0 正常  1快过期 2已过期
                if(status == 0){
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp1);
                    tvDate.setTextColor(Color.parseColor("#458bd0"));
                }else if(status == 1){
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp1);
                    tvDate.setTextColor(Color.parseColor("#458bd0"));
                }else if(status == 2){
                    frameLayout.setBackgroundResource(R.mipmap.onecom_stamp3);
                    tvDate.setTextColor(Color.parseColor("#FF0202"));
                }
            }
            if (safeTrainVo.getNext_date() != null) {
                tvDate.setText("下次培训日期\n" + CommonUtil.secondToTimeStr(safeTrainVo.getNext_date(), DateTypeEnum.DAY));
            }
        }
    }
}


